var eventsMgr = require('eventsMgr');
var consts = require('consts');
var yySpine = require('yySpine');
var actions = require('actions');
let soundMgr = require('soundMgr');

cc.Class({
    extends: cc.Component,

    properties: {
        WordPosition:cc.Node,
        heartNum:0,
        linHeartNum:0,
        isdead:false,
        feiJi:cc.Node,
        speed:10,
        movetype:0,
    },

    onLoad() {
        this.WordPosition = cc.find('Canvas');

    },

    onCollisionEnter: function (other, self) {
        switch (self.node.name) {
            case "feiji":
                if(other.node.name == "guaiwu"){
                    eventsMgr.emit(consts.EVENTS.FeiJi);
                }
                break;
            case "zidan":
                if(other.node.name == "guaiwu"){
                    self.node.stopAllActions();
                    self.node.x += 50;
                    self.node.children[0].active = false;
                    self.node.children[1].active = true;
                    yySpine.playAnimation(self.node.children[1],"boom",1,true,function () {
                        yySpine.playAnimation(self.node.children[1],"idle",1,true);
                        self.node.children[1].active = false;
                        self.node.destroy();
                    })
                }
                break;
            case "guaiwu":
                if(other.node.name == "zidan"){
                    soundMgr.playSoundEffectByName("aida");
                    this.GuaiWuDiaoXue();
                }
                break;
            case "hedan":
                eventsMgr.emit(consts.EVENTS.HeDan);
                break;
            case "jiaxue":
                eventsMgr.emit(consts.EVENTS.JiaXue);
                break;
        }


        // if(other.node.name == "gongjian"){
        //     this.heartNum--;
        //     other.node.active = false;
        //     if(self.node.name == "diren2" && this.heartNum == 1){
        //         eventsMgr.emit(consts.EVENTS.JinBi,{"data":self});
        //     }
        //     if(this.heartNum == 0){
        //         eventsMgr.emit(consts.EVENTS.Heart,{"data":self});
        //     }
        // }

    },

    GuaiWuDiaoXue(){
        let self = this;
        if(self.isdead){
            return;
        }
        self.linHeartNum--;
        if(self.heartNum == 3){
            if(self.linHeartNum == 2){
                self.node.children[0].children[0].children[0].width = 66;
                yySpine.playAnimation(self.node,"damges",1,true,function () {
                    yySpine.playAnimation(self.node,"idle",0,true)
                })
            }else if(self.linHeartNum == 1){
                self.node.children[0].children[0].children[0].width = 33;
                yySpine.playAnimation(self.node,"damges",1,true,function () {
                    yySpine.playAnimation(self.node,"idle",0,true)
                })
            }else if(self.linHeartNum == 0){
                self.isdead = true;
                self.node.name = "dead";
                self.node.children[0].children[0].children[0].width = 0;
                eventsMgr.emit(consts.EVENTS.GuaiWu);
                actions.fadeOut(self.node.children[0],0.5);
                actions.fadeOut(self.node,0.5,function () {
                    self.node.active = false;
                });
                yySpine.playAnimation(self.node,"dead",1,true)
            }
        }else if(self.heartNum == 2){
            if(self.linHeartNum == 1){
                self.node.children[0].children[0].children[0].width = 50;
                yySpine.playAnimation(self.node,"damage",1,true,function () {
                    yySpine.playAnimation(self.node,"idle",0,true)
                })
            }else if(self.linHeartNum == 0){
                self.isdead = true;
                self.node.name = "dead";
                self.node.children[0].children[0].children[0].width = 0;
                eventsMgr.emit(consts.EVENTS.GuaiWu);
                actions.fadeOut(self.node.children[0],0.5);
                actions.fadeOut(self.node,0.5,function () {
                    self.node.active = false;
                });
                yySpine.playAnimation(self.node,"dead",1,true)
            }
        }else if(self.heartNum == 1){
            if(self.linHeartNum == 0){
                self.isdead = true;
                self.node.name = "dead";
                self.node.children[0].children[0].children[0].width = 0;
                eventsMgr.emit(consts.EVENTS.GuaiWu);
                actions.fadeOut(self.node.children[0],0.5);
                actions.fadeOut(self.node,0.5,function () {
                    self.node.active = false;
                });
                yySpine.playAnimation(self.node,"dead",1,true)
            }
        }
    },


    update (dt) {
        let self = this;
        if(self.node.name == "guaiwu"){
            if(window.canMove == this.movetype){
                if(self.node.x - 150 > self.feiJi.x){
                    self.node.x -= dt * this.speed;
                }
                if(self.node.y > self.feiJi.y - 50){
                    self.node.y -= dt * this.speed;
                }else if(self.node.y < self.feiJi.y){
                    self.node.y += dt * this.speed;
                }
            }
        }
    },


});
